home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / Apps / DevTools / ClassEditor.0.4 / Source / RZBrowserCell.subproj / RZTextToken.m < prev   
Encoding:
Text File  |  1995-04-04  |  2.4 KB  |  114 lines

  1. /* 
  2.  * RZTextToken - support object for the RZBrowserCell that represents
  3.  *     a piece of text with a particular font and color
  4.  *
  5.  * You may freely copy, distribute and reuse the code in this example.
  6.  * This code is provided AS IS without warranty of any kind, expressed 
  7.  * or implied, as to its fitness for any particular use.
  8.  *
  9.  * Copyright 1995 Ralph Zazula (rzazula@next.com).  All Rights Reserved.
  10.  *
  11.  */
  12.  
  13. #import "RZTextToken.h"
  14. #import "RZSimpleString.h"
  15. #import <appkit/NXImage.h>
  16. #import <stdio.h>
  17. #import <stdarg.h>
  18.  
  19. #define FONT_STYLE_NORM 0
  20. #define FONT_STYLE_BOLD 1
  21. #define FONT_STYLE_ITALIC 2
  22. #define FONT_STYLE_BOLD_ITALIC 3
  23.  
  24. #define DEFAULT_FONT FONT_STYLE_NORM
  25. #define DEFAULT_COLOR NX_BLACK
  26.  
  27. @implementation RZTextToken
  28.  
  29. - init
  30. {
  31.     return [self notImplemented:_cmd];
  32. }
  33.  
  34. - initText:(const char *)format at:(unsigned)i,...
  35. {
  36.     va_list args;
  37.     static char buf[1024];
  38.     
  39.     va_start(args, format);
  40.     vsprintf(buf, format, args);
  41.     
  42.     return [self initText:buf at:i font:DEFAULT_FONT color:DEFAULT_COLOR];
  43. }
  44.  
  45. - initText:(const char *)format at:(unsigned)i font:(char)font color:(char)color,...
  46. {
  47.     va_list args;
  48.     static char buf[1024];
  49.     
  50.     va_start(args, format);
  51.     vsprintf(buf, format, args);
  52.  
  53.     return [self initData:buf at:i isText:YES font:font color:color];
  54. }
  55.  
  56. - initImage:(const char *)d at:(unsigned)i
  57. {
  58.     return [self initData:d at:i isText:NO font:0 color:0];
  59. }
  60.  
  61. - initData:(const char *)d at:(unsigned)i isText:(BOOL)isText font:(char)font color:(char)color
  62. {
  63.     if(self = [super init]) {
  64.         if(isText) {
  65.             data = [[RZSimpleString alloc] initWith:d];
  66.         } else {
  67.             data = [NXImage findImageNamed:d];
  68.             if(!data) {
  69.                 /* see if it's a filename */
  70.                 data = [[NXImage alloc] init];
  71.                 if(![data loadFromFile:d]) {
  72.                     data = [data free];
  73.                 }
  74.             }
  75.         }
  76.         if(data) {
  77.             index = i;
  78.             _flags.text = isText;
  79.             _flags.font = font;
  80.             _flags.color = color;
  81.         }
  82.     }
  83.     return self;
  84. }
  85.  
  86. - (unsigned)position    { return index; }
  87. - data                    { return data; }
  88. - (BOOL)isText            { return _flags.text; }
  89. - (char)font            { return _flags.font; }
  90. - (char)color            { return _flags.color; }
  91.  
  92. - write:(NXTypedStream *)ts
  93. {
  94.     [super write:ts];
  95.     NXWriteTypes(ts, "i@i", &index, &data, _flags);
  96.     return self;
  97. }
  98.  
  99. - read:(NXTypedStream *)ts
  100. {
  101.     [super read:ts];
  102.     NXReadTypes(ts, "i@i", &index, &data, _flags);
  103.     return self;
  104. }
  105.  
  106. @end
  107.  
  108. @implementation RZTextToken(RZSortableObjects)
  109. - (unsigned)sortIndex
  110.     return index; 
  111. }
  112. @end
  113.